package com.aliyunlog.utils;

import oi.thekraken.grok.api.Grok;
import oi.thekraken.grok.api.Match;
import oi.thekraken.grok.api.exception.GrokException;

/**
 * Created by linqunxuan on 2017/6/29.
 */
public class GrokTest {


    public static void main(String[] a) throws GrokException {

        /**
         * 参考
         * http://www.voidcn.com/blog/u013390476/article/p-4968947.html
         */
        // Get an instance of grok
        Grok grok = Grok.EMPTY;

// add a pattern to grok
//        grok.addPattern("foo", "\\w+");
        grok.addPatternFromFile("E:\\git_repo\\log_monitor\\patterns\\java");
//        grok.addPattern("fromIP", "%{IPV4}");
        // compile 一个 pattern，期间我被空格坑了一下
        grok.compile("\\[%{JAVACLASS:class}\\]");


// compile and add semantic
//        grok.compile("\\[%{JAVACLASS}\\]");

// Match with some log
        Match m = grok.match("11803#0: *143348505 upstream timed out (110: Connection timed out) while reading response header from upstream, \n" +
                "client: 58.19.214.140, server: outputcangyibao.alog.cc, request: \"POST /report/reportSaleOrderCount.htm HTTP/1.1\", \n" +
                "upstream: \"http://10.161.136.66:8866/report/reportSaleOrderCount.htm\", host: \"outputcangyibao.alog.cc:8686\", \n" +
                "referrer: \"http://outputcangyibao.alog.cc:8686/report/reportSaleOrderCount.htm\"");
        m.captures();
// ^([a-zA-Z]+[.][a-zA-Z]+)[.]*.*
// Print
        System.out.println(m.toMap());
// Then the output should be
// {bar=hello}
    }
    
    
    public static boolean lll(){
    	return false;
    }
}
